/**
 * 
 */
package com.vurenium.ras.util;

import java.util.BitSet;

/**
 * @author svaish
 *
 */
public class rasBitSet extends BitSet {

	public static void main (String args[]){
		rasBitSet x = new rasBitSet(args[0]);
		System.out.println(x.toBinaryString());
		x.shiftRight(2);
		System.out.println("right"+x.toBinaryString());	
		x.shiftLeft(2);
		System.out.println("left"+ x.toBinaryString());
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	public rasBitSet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param arg0
	 */
	public rasBitSet(int arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	/**
	 * Converts the string to BitSet. Only for Strings that are
	 * created by this.toString() 
	 */
	public rasBitSet(String str) {
		char[] chrs = str.toCharArray();
		for (int i=0; i<chrs.length; i++) {
			if (chrs[i] == '1'){
				this.set(i,true);
			}else {
				this.set(i, false);
			}
		}
	}
	/*
		byte[] bytes = str.getBytes();
        for (int i=0; i<bytes.length*8; i++) {
            if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
                this.set(i);
            }
        }
	*/

	/**
	 * 
	 */
	public void shiftRight(int by)
	{
		for (int k=0; k<by; k++){
			for (int i=this.length()-1; i>=1; i--){
				this.set(i, this.get(i-1));
			}
			this.set(0, false);
		}
	}
	
	/**
	 * 
	 */
	public void shiftLeft(int by)
	{
		for (int k=0; k<by; k++){
			for (int i=1;i<=this.length()-1;i++){
			this.set(i-1, this.get(i));
			}
			this.set(this.length()-1, false);
		}
	}
	/**
	 * Converts the BitSet to a string 
	 * for storing in the DB
	 */
	public String toBinaryString()
	{
       char[] str = new char[this.length()];
       for (int i=0; i<this.length(); i++) {
    	   if (this.get(i)){
    		   str[i] = '1';
    	   }else{
    		   str[i] = '0';
    	   }
       }
       return new String (str);
	}
	
	/*
		byte[] bytes = new byte[this.length()/8+1];
        for (int i=0; i<this.length(); i++) {
            if (this.get(i)) {
                bytes[bytes.length-i/8-1] |= 1<<(i%8);
            }
        }
		return new String(bytes);
	*/
	
}
